<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/28
 * Time: 11:48
 */

namespace app\api\controller;


use app\common\controller\Base;
use think\Db;
use app\common\logic\Trend;
use think\cache\driver\Redis;

header("ACCESS-CONTROL-ALLOW-ORIGIN:http://114.215.68.205");

class Lottery extends Base{

    public function _initialize(){
        parent::_initialize();
        $start = array(
            'user_all' => $this->user_all,
            'app_down' => $this->app_down,
            'logo' => $this->logo
        );
        $kjjg_id = input('get.kjjg_id');
        //判断彩种是否开启
        if(($kjjg_id && $this->lottery_if_open($kjjg_id) != 1)) {
            return '<script>alert("抱歉，您无权访问该页面！");</script>';
        }
        if($kjjg_id) {
            $kj_title = Db::name('caipai_list')->where('id=' . $kjjg_id)->field('cp_name_old,cp_name_new')->find();
            $this->kj_title = $kj_title['cp_name_new'] == '' ? $kj_title['cp_name_old'] : $kj_title['cp_name_new'];
        }
        $this->assign($start);
        $this->assign('cur','lottery');
        //验证加密
        $VALUE_KEY = ['afewfewfew21342dsaewf'=>'8f4da4f9ew4f98ewq65fwe48f9w'];
        $HEADER_TIME = isset($_SERVER['HTTP_TIME']) ? $_SERVER['HTTP_TIME'] : '';
        $HEADER_KEY = isset($_SERVER['HTTP_KEY']) ? $_SERVER['HTTP_KEY'] : '';
        $HEADER_SIGN = isset($_SERVER['HTTP_SIGN']) ? $_SERVER['HTTP_SIGN'] : '';

        $POST_LOGIN_KEY = isset($_SERVER['HTTP_LOGINKEY']) ? $_SERVER['HTTP_LOGINKEY'] : ( isset($_POST['login_key']) ? $_POST['login_key'] : '' );
        $POST_NAME = isset($_SERVER['HTTP_USERNAME']) ? $_SERVER['HTTP_USERNAME'] : (isset($_POST['username']) ? $_POST['username'] : (isset($_POST['name']) ? $_POST['name']:''));


        if( !isset($VALUE_KEY[$HEADER_KEY]) ){
            exit(json_encode(['result'=>0], JSON_UNESCAPED_UNICODE));
        }

        $validation =  $HEADER_TIME.$VALUE_KEY[$HEADER_KEY].$POST_LOGIN_KEY.$POST_NAME;
        if( $HEADER_SIGN != md5($validation) ){
            exit(json_encode(['result'=>0], JSON_UNESCAPED_UNICODE));
        }
    }
    /**
     * 开奖公告列表
     * @return mixed
     */
    public function newList(){

        $max_id_query = Db::name('kjjg_list')
            ->field('MAX(id)')
            ->where('kjjg','>','0')
            ->group('czid')
            ->buildSql();

        $max_czid_query = Db::name('kjjg_list')
            ->field('czid,kjjg,qihao,jztime')
            ->where("id IN({$max_id_query})")
            ->buildSql();
        $caipai_list = Db::name('caipai_list')
            ->alias('a')
            ->join("{$max_czid_query} b",'b.czid=a.id')
            ->field('a.id,a.cp_name_old,a.cp_name_new,b.kjjg,b.qihao,b.jztime')
            ->where('a.cp_if_open<2')
            ->order('a.cp_mb_px desc')
            ->select();
        $cp_id = [];
        foreach ($caipai_list as &$caipai_single) {
            $cp_id[] = $caipai_single['id'];
            $caipai_single['cp_name'] = !empty($caipai_single['cp_name_new']) ? $caipai_single['cp_name_new'] : $caipai_single['cp_name_old'];
            $caipai_single['kjjg'] = kjjg_split($caipai_single['kjjg'], $caipai_single['id']);
            $a = 0;
            foreach ($caipai_single['kjjg'] as $ss){
                $a += $ss;
            }
           /* if( 36==$caipai_single['id'] || 44==$caipai_single['id']){
                $caipai_single['kjjg'][] = '=';
                $caipai_single['kjjg'][] = $a;
            }*/
        }
        unset($caipai_single);
        $this->assign('cp_id',json_encode($cp_id));
        $this->assign('caipai_list', $caipai_list);
        return  $this->fetch('new_list');
    }
    /**
     * 开奖公告详情
     * @return mixed
     */
    public function newDetail(){
        $kjjg_id = input('get.kjjg_id');
        $sql = 'SELECT *
                FROM (
                        SELECT jg.czid,jg.kjjg,jg.qihao,jg.jztime,cp.cp_name_old,cp.cp_name_new,cp.cp_mb_px,cp.id
                        FROM kkty_kjjg_list jg
                        LEFT JOIN kkty_caipai_list cp
                        ON jg.czid=cp.id
                        WHERE cp.id='.$kjjg_id.' and jg.kjjg="0"
                        ORDER BY jg.jztime desc
                ) as t limit 1';
        $new_detail = Db::query($sql);
        if( empty($new_detail) ){
            return '<script>alert("暂无更新数据")</script>';
        }
        $new_detail = $new_detail[0];
        $new_detail['cp_name'] = !empty($new_detail['cp_name_new']) ? $new_detail['cp_name_new'] : $new_detail['cp_name_old'];
        $list = Db::name('kjjg_list')
            ->where('czid= ' . $kjjg_id . ' and kjjg>0')
            ->order('qihao desc')
            ->limit(20)
            ->select();

        foreach ($list as &$kjls){
            $kjls['kjjg'] = kjjg_split($kjls['kjjg'], $kjls['czid']);
            $a=0;
            /*foreach ($kjls['kjjg'] as $ss){$a+=$ss;}
            if($kjls['czid']==36 || $kjls['czid']==44){
                $kjls['kjjg'][]='=';
                $kjls['kjjg'][]=$a;
            }*/
        }
		$kjjg_data_fur = Db::name('kjjg_list')->field('jztime')->where('czid',$kjjg_id)->where('jztime','>',time())->where('kjjg','0')->order('qihao','DESC')->find();
        $kjjg_data_fur['jztime'] = $kjjg_data_fur['jztime'] ? $kjjg_data_fur['jztime'] : time();
        $jztime_fur = $kjjg_data_fur['jztime'] - time();
        if($jztime_fur<=0){
        	$h_m_s['h']='开';
        	$h_m_s['m']='奖';
        	$h_m_s['s']='中';
        }else{
        	$h_m_s['h'] = floor(($jztime_fur / 3600) % 24);
        	$h_m_s['m'] = floor(($jztime_fur / 60) % 60);
        	$h_m_s['s'] = floor($jztime_fur % 60);
        	if($h_m_s['h'] <10) $h_m_s['h'] = '0'.$h_m_s['h'];
        	if($h_m_s['m'] <10) $h_m_s['m'] = '0'.$h_m_s['m'];
        	if($h_m_s['s'] <10) $h_m_s['s'] = '0'.$h_m_s['s'];
        }
        $this->assign('h_m_s',$h_m_s);
        $this->assign('jztime_fur',$jztime_fur);
        unset($kjls);
        $kjjg_data_fur = Db::name('kjjg_list')->field('jztime')->where('czid',$kjjg_id)->where('jztime','>',time())->where('kjjg','0')->order('qihao','DESC')->find();
        $kjjg_data_fur['jztime'] = $kjjg_data_fur['jztime'] ? $kjjg_data_fur['jztime'] : time();
        $jztime_fur = $kjjg_data_fur['jztime'] - time();
        if($jztime_fur<=0){
        	$h_m_s['h']='开';
        	$h_m_s['m']='奖';
        	$h_m_s['s']='中';
        }else{
        	$h_m_s['h'] = floor(($jztime_fur / 3600) % 24);
        	$h_m_s['m'] = floor(($jztime_fur / 60) % 60);
        	$h_m_s['s'] = floor($jztime_fur % 60);
        	if($h_m_s['h'] <10) $h_m_s['h'] = '0'.$h_m_s['h'];
        	if($h_m_s['m'] <10) $h_m_s['m'] = '0'.$h_m_s['m'];
        	if($h_m_s['s'] <10) $h_m_s['s'] = '0'.$h_m_s['s'];
        }
        $this->assign('h_m_s',$h_m_s);
        $this->assign('jztime_fur',$jztime_fur);

        $syiqi = Db::name('kjjg_list')->where('czid',$kjjg_id)->where('jztime','<',time())->where('kjjg','<>','0')->field("qihao,jztime")->order('qihao','DESC')->find();
        $between =  Db::name('kjjg_list')->where('czid',$kjjg_id)->where('jztime','>',$syiqi['jztime'])->where('jztime','<',$kjjg_data_fur['jztime'])->count();
        $this->assign('has',$between);
        $this->assign('syiq',$syiqi['qihao']);

        $this->assign('caipiao',$new_detail);
        $this->assign('kjjg_id',$kjjg_id);
        $this->assign('list', $list);

        $from = input('from',0);
        $this->assign('from', $from);

        return $this->fetch('new_detail');
    }

    /**
     * 开奖走势详情
     *
     * @return \think\mixed
     */
    public function trendDetails()
    {   $game_id = input('game_id');
        $this->assign('game_id',$game_id);
        $this->assign('cur', 'lottery_trend');
        $this->assign('title','走势图');

        return $this->fetch('trend_details');
    }
    public function zoushi(){
        $game_id = input('game_id');
        $this->assign('game_id',$game_id);
        $zoushi = Db::name('kjjg_list')->where('czid',$game_id)->where("kjjg>'0'")->order('qihao','DESC')->limit(24)->select();
        $trend = new Trend();
        if($game_id == 1 || $game_id == 8 || $game_id == 62){
            $zoushi = $trend->getSscTrend($zoushi,$game_id);
        }else if($game_id == 4 || $game_id == 61 || $game_id == 63){
            $zoushi = $trend->getPk10Trend($zoushi,$game_id);
        }else if($game_id == 36 || $game_id == 44 || $game_id == 72){
            $zoushi = $trend->getK3Trend($zoushi,$game_id);
        }else if($game_id == 16 || $game_id == 25 || $game_id == 28){
            $zoushi = $trend->get11x5Trend($zoushi,$game_id);
        }else if($game_id == 5 || $game_id == 7 || $game_id == 48){
            $zoushi = $trend->getDpcTrend($zoushi,$game_id);
        }else if($game_id == 10){
            $zoushi = $trend->getPcddTrend($zoushi,$game_id);
        }else if($game_id == 9){
            $zoushi = $trend->getHk6Trend($zoushi,$game_id);
        }else if($game_id == 51){
            $zoushi = $trend->getQxcTrend($zoushi,$game_id);
        }

        $this->assign('zoushi',$zoushi);
        $this->assign('title','走势图');

        return $this->fetch();
    }
    /* 彩种介绍 */
    public function guize(){
        $gameid = input('game_id') ? input('game_id') : '';
        $gid = input('gid') ? input('gid') : 1;
        if($gameid) {
            $info = Db::name('lottery_classify')->field('id,class_name_lottery')->where("czids REGEXP '(^|,){$gameid}($|,)'")->find();
            $class_name_lottery = $info['class_name_lottery'];
            $gid = $info['id'];
        } else {
            $info = Db::name('lottery_classify')->field('id,class_name_lottery')->where('id',$gid)->find();
            $class_name_lottery = $info['class_name_lottery'];
            $gid = $info['id'];
        }
        $this->assign('gid',$gid);
        $this->assign('class_name_lottery',$class_name_lottery);
        return $this->fetch();
    }
    public function zaixiankefu(){


        return $this->fetch();
    }

    /* 玩法规则 */
    public function gz_list(){
        $gameid = input('game_id') ? input('game_id') : '';
        $gid = input('gid') ? input('gid') : '1';
        if($gameid) {
            $cp = Db::name('lottery_classify')->where("czids REGEXP '(^|,){$gameid}($|,)'")->find();
            $wanfalist = Db::name('play_func')->where('czid',$gameid)->select();
            $this->assign('game_id',$gameid);
        } else {
            $cp = Db::name('lottery_classify')->where('id',$gid)->find();
            $czid_arr = explode(',',$cp['czids']);
            $wanfalist = Db::name('play_func')->where('czid',$czid_arr[0])->select();
            $this->assign('game_id',$czid_arr[0]);
        }

        $this->assign('cp',$cp);
        $this->assign('wflist',$wanfalist);
        return $this->fetch();
    }
    public function zhudan(){
        $game_id = input('game_id') ? input('game_id') : 'all';
        $caizhong = Db::name('caipai_list')->where('cp_if_open',1)->field('id,cp_name_old,cp_name_new')->select();
        if(input('get.game_id')){
            $tit = Db::name('caipai_list')->where('id',$game_id)->field('id,cp_name_old,cp_name_new')->find();
            $this->assign('tit',$tit);
        }
        $this->assign('game',$game_id);
        $this->assign('caizhong',$caizhong);
        $this->assign('cur','lottery_trend');
        return $this->fetch();
    }

    public function zhudanData(){

        $game_id = input('game_id') ? input('game_id') : 'all';
        $type = input('get.type') ? input('get.type') : 'all';
        $page = input('get.page') ? input('get.page') : 0;
        if($game_id == 'all'){
            $where = array('username'=>$this->user_all['username']);
            $w_yzj = array('username'=>$this->user_all['username'],'zjje'=>['>',0.001]);
        }else{
            $where = array('username'=>$this->user_all['username'],'czid'=>$game_id,);
            $w_yzj = array('username'=>$this->user_all['username'],'czid'=>$game_id,'zjje'=>['>',0.001]);
        }
        $data_array = array();
        $yzj=array();
        $cd=array();
        $qb_caizhong=array();
        $ok_qb = array();
        $swyzj = array();
        if($this->user_all['username']) {
            $user = Db::name('username')->where('username',$this->user_all['username'])->value('swzh');
            $page_count = $type == 'all' ? 5 : 20;

            if($type == 'all' || $type == 2){
                $qb_caizhong = Db::name('touzhu_list_no')
                    ->alias('a')
                    ->join('__CAIPAI_LIST__ b','a.czid=b.id')
                    ->field('a.id as uid,a.czid,a.qihao,a.tzhm,a.tzje,a.tzhm,a.dzje,a.tzzs,a.username,a.zjje,a.wfid,a.dqpl,a.kjjg,a.tz_time,b.cp_name_old,b.cp_name_new')
                    ->where($where)->order('qihao','DESC')->page($page,25)->select();
            }

            if($type == 'all'){
                if($user == 1){
                    $ok_qb = Db::name('touzhu_list_sw')->alias('a')->join('__CAIPAI_LIST__ b','b.id=a.czid')
                        ->field('a.id as uid,a.czid,a.qihao,a.tzhm,a.tzje,a.tzhm,a.dzje,a.tzzs,a.username,a.zjje,a.wfid,a.dqpl,a.kjjg,a.tz_time,b.cp_name_old,b.cp_name_new')
                        ->where($where)->order('qihao','DESC')->page($page,$page_count)->select();
                }else{
                    $ok_qb = Db::name('touzhu_list_ok')
                        ->alias('a')
                        ->join('__CAIPAI_LIST__ b','a.czid=b.id')
                        ->field('a.id as uid,a.czid,a.qihao,a.tzhm,a.tzje,a.tzhm,a.dzje,a.tzzs,a.username,a.zjje,a.wfid,a.dqpl,a.kjjg,a.tz_time,b.cp_name_old,b.cp_name_new')
                        ->where($where)->order('qihao','DESC')->page($page,$page_count)->select();
                }

            }
            if($type == 'all' || $type == 1){
                if($user == 1){
                    $yzj = Db::name('touzhu_list_sw')->alias('a')->join('__CAIPAI_LIST__ b','b.id=a.czid')
                        ->field('a.id as uid,a.czid,a.qihao,a.tzhm,a.tzje,a.tzhm,a.dzje,a.tzzs,a.username,a.zjje,a.wfid,a.dqpl,a.kjjg,a.tz_time,b.cp_name_old,b.cp_name_new')
                        ->where($w_yzj)->order('qihao','DESC')->page($page,$page_count)->select();
                }else{
                    $yzj = Db::name('touzhu_list_ok')->alias('a')->join('__CAIPAI_LIST__ b','b.id=a.czid')
                        ->field('a.id as uid,a.czid,a.qihao,a.tzhm,a.tzje,a.tzhm,a.dzje,a.tzzs,a.username,a.zjje,a.wfid,a.dqpl,a.kjjg,a.tz_time,b.cp_name_old,b.cp_name_new')
                        ->where($w_yzj)->order('qihao','DESC')->page($page,$page_count)->select();
                }
            }
            if($type == 'all' || $type == 3){
                $cd = Db::name('touzhu_list_zh')
                    ->alias('a')
                    ->join('__CAIPAI_LIST__ b','b.id=a.czid')
                    ->field('a.id as uid,a.czid,a.qihao,a.tzhm,a.tzje,a.tzhm,a.dzje,a.tzzs,a.username,a.zjje,a.wfid,a.dqpl,a.kjjg,a.tz_time,b.cp_name_old,b.cp_name_new')
                    ->where($where)->order('qihao','DESC')->page($page,$page_count)->select();
            }
            $data_array = array_merge($qb_caizhong,$ok_qb,$yzj,$cd);
            if(isset($data_array[0])){
                $data_array = arraySequence($data_array,'tz_time');
            }
        } else {
            $data_array = array();
        }
        return json($data_array);
    }
    public function runLottery()
    {
        $kjjg_id = input('get.kjjg_id');
        $max_id_query = Db::name('kjjg_list')
            ->field('MAX(id)')
            ->group('czid')
            ->buildSql();

        $max_czid_query = Db::name('kjjg_list')
            ->field('czid,kjjg,qihao,jztime')
            ->where("id IN({$max_id_query})")
            ->buildSql();

        $caipai_list = Db::name('caipai_list')
            ->alias('a')
            ->join("{$max_czid_query} b",'b.czid=a.id')
            ->field('a.id,a.cp_name_old,a.cp_name_new,b.kjjg,b.qihao,b.jztime')
            ->where('a.id',$kjjg_id)
            ->where('a.cp_if_open<2')
            ->order('a.cp_mb_px desc')
            ->find();

        $caipai_list['cp_name_new'] || $caipai_list['cp_name_new'] = $caipai_list['cp_name_old'];
        $caipai_list['cp_name'] = !empty( $caipai_list['cp_name_new'] ) ? $caipai_list['cp_name_new'] : $caipai_list['cp_name_old'];


        $list = Db::name('kjjg_list')
            ->where('czid= ' . $kjjg_id . ' and kjjg>0')
            ->order('qihao desc')
            ->limit(20)
            ->select();

        foreach ($list as &$kjls){
            $kjls['kjjg'] = kjjg_split($kjls['kjjg'], $kjls['czid']);
            $a=0;
            foreach ($kjls['kjjg'] as $ss){$a+=$ss;}
            if($kjls['czid']==36 || $kjls['czid']==44){
                $kjls['kjjg'][]='=';
                $kjls['kjjg'][]=$a;
            }
        }
        $kjjg_data_fur = Db::name('kjjg_list')->field('jztime')->where('czid',$kjjg_id)->where('jztime','>',time())->where('kjjg','0')->order('qihao','DESC')->find();
        $kjjg_data_fur['jztime'] = $kjjg_data_fur['jztime'] ? $kjjg_data_fur['jztime'] : time();
        $jztime_fur = $kjjg_data_fur['jztime'] - time();
        if($jztime_fur<=0){
        	$h_m_s['h']='开';
        	$h_m_s['m']='奖';
        	$h_m_s['s']='中';
        }else{
        	$h_m_s['h'] = floor(($jztime_fur / 3600) % 24);
        	$h_m_s['m'] = floor(($jztime_fur / 60) % 60);
        	$h_m_s['s'] = floor($jztime_fur % 60);
        	if($h_m_s['h'] <10) $h_m_s['h'] = '0'.$h_m_s['h'];
        	if($h_m_s['m'] <10) $h_m_s['m'] = '0'.$h_m_s['m'];
        	if($h_m_s['s'] <10) $h_m_s['s'] = '0'.$h_m_s['s'];
        }
        $this->assign('h_m_s',$h_m_s);
        $this->assign('jztime_fur',$jztime_fur);

        $redis = new Redis();
        $before_qihao = $redis->get('secend_'.$kjjg_id);
        if(!$before_qihao) {
            $before_qihao = Db::name('kjjg_list')->where('czid',$kjjg_id)->where('jztime','<',$kjjg_data_fur['jztime'])->order('qihao','DESC')->value('qihao');
        }
        $between =  Db::name('kjjg_list')->where(['czid'=>$kjjg_id,'qihao'=>$before_qihao])->value('kjjg');

        $this->assign('has',empty($between) ? 1 : 0);
        $this->assign('syiq',$before_qihao);

        $this->assign('caipiao',$caipai_list);
        $this->assign('kjjg_id',$kjjg_id);
        $this->assign('title','开奖公告');
        $this->assign('list', $list);
        return $this->fetch('kjjg_noe');
    }
}